<template>
    <div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.keyword" placeholder="设备名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="func_handleSearch" plain>搜索</el-button>
                <el-button type="success" class="mr10" icon="el-icon-search" @click="func_handleAdd" plain>添加</el-button>
				<el-dropdown :hide-on-click="false">
                    <el-button class="green mr10" plain>操作</el-button>
					<template #dropdown>
						<el-dropdown-menu>
                            <el-dropdown-item type="text" @click="func_handleBatchDel" class="red "> 批量删除</el-dropdown-item>
						</el-dropdown-menu>
					</template>
				</el-dropdown>
            </div>
            <el-table :data="MemberIntelTableData" border class="table" header-cell-class-name="table-header" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" />
                <el-table-column prop="id" label="ID" width="55" align="center" v-if="false" />
				<el-table-column label="序号" width="80" align="center"><template #default="scope">
						{{scope.$index + 1}}
				</template></el-table-column>
                <el-table-column prop="memberName" label="点击查看详情" width="220" >
                    <template #default="scope">
                        <el-button type="text" @click="func_handleDrawerOpen(scope.$index, scope.row)"> <el-icon class="el-icon-s-unfold" /> {{scope.row.memberName}} </el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="memberName" label="会员名称" v-if="false"/>
                <el-table-column prop="memberId" label="会员名称" v-if="false"/>
				<el-table-column prop="integral" label="总积分值" />
                <el-table-column prop="gradeName" label="会员等级" />
                <el-table-column prop="lstIntegral" label="最新获得（兑换）积分" />
                <el-table-column prop="createTime" label="最新积分获得时间" />
                <el-table-column prop="strIntegralWay" label="最新积分获得途径" />
				<el-table-column prop="integralWay" label="最新积分获得途径" v-if="false"/>
                <!-- <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button type="text" icon="el-icon-edit" @click="func_handleEdit(scope.$index, scope.row)">编辑
                        </el-button>
						<el-button type="text" icon="el-icon-delete" class="red" @click="func_handleDelete(scope.$index, scope.row)">删除
						</el-button>
                    </template>
                </el-table-column> -->
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.pageNum"
                    :page-size="query.pageSize" :total="dataTotal" @current-change="handlePageChange"></el-pagination>
            </div>
        </div>
        <!-- 编辑弹出框 -->
        <el-dialog title="编辑会员等级" v-model="editDialogVisible" width="55%" :close-on-click-modal="false">
            <VMemberIntel v-bind:selectValue="VMemberIntelData" @MemberIntelData="func_loadVMemberIntelData" />
        </el-dialog>
        <!-- 详细抽屉 -------------------------------------------------------------------------------------------------------------- -->
        <el-drawer title="详细信息" v-model="drawerViewVisible " direction="rtl" size="68%">
            <div class="container">
                <div class="handle-box">
                    <el-table :data="memberDrawerIntelTableData" border class="table" header-cell-class-name="table-header">
                        <el-table-column prop="id" label="ID" width="55" align="center" />
                        <el-table-column prop="memberName" label="会员名称" />
                        <el-table-column prop="memberId" label="会员名称" v-if="false"/>
                        <el-table-column prop="integral" label="获得（兑换）积分" />
                        <el-table-column prop="createTime" label="积分获得时间" />
                        <el-table-column prop="strIntegralWay" label="积分获得途径" />
                        <el-table-column prop="integralWay" label="积分获得途径" v-if="false"/>
                        <el-table-column label="操作" width="180" align="center">
                            <template #default="scope">
                                <el-button type="text" icon="el-icon-delete" class="red" @click="func_handleDelete(scope.$index, scope.row)">删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination">
                        <el-pagination background layout="total, prev, pager, next" :current-page="query.pageNum"
                            :page-size="query.pageSize" :total="drawerDataTotal" @current-change="handlePageChange"></el-pagination>
                    </div>
                </div>
            </div>
        </el-drawer>
    </div>
</template>

<script>
import { ref, reactive, toRefs } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { findMemberIntelListPage, deleteMemberIntelById, batchDeleteMemberIntel, findMemberIntelListByMemberId } from "../../../api/hairmanager/memberIntel";
import { NO_DATA_SELECT_ERROR, SUCCESS_ADDDATA_DIALOG_MESSAGE, SUCCESS_DELDATA_DIALOG_MESSAGE, SUCCESS_EDITDATA_DIALOG_MESSAGE, formLabelWidth, } from "../../../api/CommonErrorMessage.js";

import VMemberIntel from "./components/memberIntelForm.vue";

export default {
    name: "borderTable",
    components: {VMemberIntel, },
    setup() {
        const router = useRouter();
		const route = useRoute();
        const formLabelWidth = ref("170px");
        const optColumnWidth = 150;
        const query = reactive({
            keyword: "",
            pageNum: 1,
            pageSize: 10,
        });
        const MemberIntelTableData = ref([]);
        const dataTotal = ref(0);
        const multipleSelection = ref([]);
        const VMemberIntelData = reactive({
            load: false,
            id: 0,
        });
        let idx = -1;
        // =========================================================================================================
        // 主业务处理 ========================================================================================= START
        /**
         * @name 获取表格数据
         */
        const func_getData = () => {
            findMemberIntelListPage(query).then((res) => {
                MemberIntelTableData.value = res.data.list;
                dataTotal.value = res.data.total;
            });
        };
        func_getData();
        /**
         * @name 主表格查询操作
         */
        const func_handleSearch = () => {
            query.pageNum = 1;
            func_getData();
        };
        /**
         * @name 分页导航
         */
        const handlePageChange = (val) => {
            query.pageNum = val;
            func_getData();
        };
        /**
         * @name 删除操作
         */
        const func_handleDelete = (index, row) => {
            // 二次确认删除
            ElMessageBox.confirm("确定要删除吗？", "提示", {
            type: "warning",
            }).then(() => {
                deleteMemberIntelById(row['id']).then((res) => {
                    if(res.code != 200) {
                        if(res.type === null){
                            ElMessage.error(res.message);
                        }else if(res.type === 0) {
                            ElMessage.warning(res.message);
                        } else {
                            ElMessage.error(res.message);
                        }
                    } else {
                        ElMessage.success(SUCCESS_DELDATA_DIALOG_MESSAGE);
                        // drawerViewVisible.value = false;
                        func_loadDrawerIntelTableData();
                        func_getData();
                    }
                });
            }).catch(() => {});
        };
        /**
         * @name 添加数据
         */
        const func_handleAdd = () => {
            VMemberIntelData['id'] = 0;
            VMemberIntelData['load'] = false;
            editDialogVisible.value = true;
        };
        /**
         * @name 行编辑操作
         */
        const func_handleEdit = (index, row) => {
            idx = index;
            VMemberIntelData['id'] = row['id'];
            VMemberIntelData['load'] = true;
            editDialogVisible.value = true;
        };
        const handleSelectionChange = (val) => {
            let ids = [];
            val.forEach((element) => {
                ids.push(element['id']);
            });
            multipleSelection.value = ids;
        };
        /**
         * @name 批次删除数据
         */
        const func_handleBatchDel = () => {
            if(multipleSelection.value.length ===0){
                ElMessage.warning(NO_DATA_SELECT_ERROR);
                return false;
            }
            // 二次确认删除
            ElMessageBox.confirm("确定要删除吗？", "提示", {
            type: "warning",
            }).then(() => {
                var obj = multipleSelection.value;
                batchDeleteMemberIntel(obj).then((res) => {
                    if(res.code != 200) {
                        if(res.type == 0) {
                            ElMessage.warning(res.message);
                        } else {
                            ElMessage.error(res.message);
                        }
                    } else {
                        ElMessage.success(SUCCESS_DELDATA_DIALOG_MESSAGE);
                        func_getData(); // 刷新数据
                    }
                });
            });
        }
        // 主业务处理 ========================================================================================= END
		// 扩展业务处理 ========================================================================================= START
		// 表格编辑时弹窗和保存
        const editDialogVisible = ref(false);
        const func_loadVMemberIntelData = (v) => {
            if(v['state'] === 1) {
                editDialogVisible.value = false;
                VMemberIntelData['load'] = v['load'];
                func_getData();
            } else {
                editDialogVisible.value = true;
            }
        };
		// 扩展业务处理 ========================================================================================= END
		// 扩展业务处理 ========================================================================================= START
        const memberDrawerIntelTableData = ref([]);
        const drawerDataTotal = ref(0);
		// 表格编辑时弹窗和保存
        const drawerViewVisible = ref(false);
        const drawer_query = reactive({
            memberId:0, pageSize: 0, pageNum: 0
        });
        const func_handleDrawerOpen = (index, row) => {
            drawerViewVisible.value = true;
            drawer_query['memberId'] = row['memberId'];
            func_loadDrawerIntelTableData();
        };
        const func_loadDrawerIntelTableData = () => {
            findMemberIntelListByMemberId(drawer_query).then((res) => {
                memberDrawerIntelTableData.value = res.data.list;
                drawerDataTotal.value = res.data.total;
            });
        };
        const drawerHandlePageChange = (val) => {
            drawer_query.pageNum = val;
            func_loadDrawerIntelTableData();
        };
		// 扩展业务处理 ========================================================================================= END
        // =========================================================================================================
        return {
            // 系统变量
            optColumnWidth, formLabelWidth, query, MemberIntelTableData, dataTotal, multipleSelection, editDialogVisible,
            // 主表格调用方法
            func_handleSearch, handlePageChange, func_handleDelete, func_handleAdd, func_handleEdit, func_handleBatchDel,
            handleSelectionChange,
            // 插件业务调用方法
			VMemberIntelData, func_loadVMemberIntelData,
            // 插件业务调用方法
            drawerViewVisible, func_loadDrawerIntelTableData, memberDrawerIntelTableData, drawerDataTotal, drawer_query,
            func_handleDrawerOpen, drawerHandlePageChange,
        };
    },
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>